package com.hy.util;

import java.io.InputStream;
import java.security.MessageDigest;

public class SHA1Utils {
	public static byte[] checksum(InputStream in) {
//		byte[] digest = md.digest();
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            byte[] block = new byte[4096];
            int length;
            while ((length = in.read(block)) > 0) {
                digest.update(block, 0, length);
            }
            return digest.digest();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	//  将计算得到的字节数组转为十六进制字符串
	public static String checksumToString(InputStream in) {
		byte[] checksum = checksum(in);
		StringBuffer sb = new StringBuffer(checksum.length);
	    String sTmp;
		for (int i = 0; i < checksum.length; i++) {
	        sTmp = Integer.toHexString(0xFF & checksum[i]);
	        if (sTmp.length() < 2) {
	            sb.append(0);
	        }
	        sb.append(sTmp.toUpperCase());
	    }
		return sb.toString();
    }
}
